$(function () {
	//用户管理
	//----用户管理
	/**
     * Init
     *
     * 
     * |-- 
     * |   `-- 
     * |   `-- 
     * |	   `-- 
     * |		   `-- 
     * |		   `-- 
     * |			   `-- 
     * |		   `-- 
     * |-- 
     * |-- 
     */
	var ad = appData();
	
	var tmplObj = null;
	var arrys = []//删除的数组
	var newData = []
	console.log('已加载')
	
	DomNameStart()
	function DomNameStart() {
		
		  $("#search2").on("click",function(){
		    $(".advanced-search").toggle(200);
		  })  
		//侧面添加class、索引加载
		$('.sys-sidebar ul li').eq(0).find('ul li ').eq(0).addClass('active')
		
		var name = $('.sys-sidebar').find('li .active a').text()
		
		$("#TopName").text(name)//替换头部日志名称
		
		
		loadDom('USER_MAN_ROLE',$("#roles"),"角色")
		loadDom('ROLE_RANK_MAN',$("#roleRank"),"")

		function loadDom(api,id,val) {
			ad.selectList(api, {},function(data) {
				
				val=='' ? $(id).empty() : $(id).empty().append("<option value='-1'> "+val+"</option>");
				
				for(var i=0;i<data.length;i++){
					$(id).append("<option value='"+data[i].id+"'>"+data[i].value+"</option>")
				}
				
				
			})
		}
		//localOptions LOCAL_MAN_NAME
		loadOptions('LOCAL_MAN_NAME',$("#localOptions"))
		function loadOptions(api,id){
			id.empty();
			
			ad.selectList(api,{},function(data){
				if(data.length==0){
					_as.jAlert('无数据')
				}else{
					//debugger
					for(var j=0;j<data.length;j++){
						function optionsL(){
							var str = '';
							if(data[j].children.length!=0){
								for(var s=0;s<data[j].children.length;s++){
									str+=`<option>${data[j].children[s].name}</option>`
								}
							}
							if(str){return str}
							
						}
						
						id.append(`<option>${data[j].name}${data[j].children.length==0?'':optionsL()}</option>`)
					}
				}
				
			})
		}
				
		
			showTable(true, 1)
			
		
		$("#delList").on('click',function(){
			if(arrys.length==0){
				_as.jAlert("请选择删除的序列")
			}else{
				ad.selectList("USER_DEL_LIST",{del:arrys},function(){
					_as.jAlert("<p>删除成功</p>")
					showTable(true,1)
					arrys = []
				})
			}
		})
		
		
		
	}
	
	domCase()
	function domCase() {
		$("#search1").on('click',function(){
			var searchVal  = $("#searchVal").val() 
			
			showTable(true,1)
		})		
		
		

		$("#save").on('click',function(){
			var tips = ['登录名称','登录密码','真实姓名','用户角色'];
			var jsT = [$("#logoName").val(),$("#logoPwd").val(),$("#realName").val(),newData]
			for(var i=0;i<jsT.length;i++){
				if(jsT[i]==''){
					if(typeof jsT[i]=='object'){
						_as.jAlert('请勾选'+tips[i]);
						return false;
					}else{
						_as.jAlert(tips[i]+'不能为空');
						return false;
					}
					
				}
			}

			var param = {
						"logoName":$("#logoName").val(),
						"logoPwd":$("#logoPwd").val(),
						"realName":$("#realName").val(),//真实姓名
						"sex":$("#sex").val(),//性别
						"phone":$("#phone").val(),//电话
						"email":$("#email").val(),//email
						"textArea":$("#textArea").val(),//备注
						"localOptions":$("#textArea").val(),//所在单位
						"roleRank":$("#textArea").val(),//职务
						"ztreeId":newData//用户角色
					}
			ad.insert("ADD_ROLE_SEND",param,function(data){
				_as.jAlert('添加成功')
			})
			
		})
		
		

		
	}
	function addDomName(tips) {
			 
		
				
				if(arrys.indexOf(tips)==-1){
					arrys.push(tips)
				}else{
					var num =  arrys.indexOf(tips);
	    				arrys.splice(num,1);
				}
				console.log(arrys)
			
	}
	var power = false
	function addDomName2() {
		arrys = []
		power = !power
		if(power){
			for(var i=0;i<$("#tableBody tr").length;i++){
				arrys.push($("#tableBody tr:eq("+i+")").attr('data-id'))
				//console.log($("#tableBody tr").attr('data-id'))
			}
		}else{
			arrys = []
		}
		
			console.log(arrys)
			
	}
	//表格show加载
	function showTable(dontRenderPager, page) {
  		
		var params = {
			"page":page,
//			"top1":top1,
//			"top2":top2,
			"keyValue":$("#searchVal").val(),
			"pageSize": appApi('PAGE_SIZE')
		}
  		
		ad.insert("USER_MAN_LIST", params, function(data) {
			
			$("#tableBody").empty();
			if (data.length === 0) {
				$("#tableBody").append("<tr><td colspan='3' class='colspanCss'><h1>暂无数据</h1></td></tr>");
				return false;
			}
			
			
			
			$.get('scripts/app/safe/userManage/UserList.tmpl', function(tmpl) {
			
				
				$("#tableBody").empty().append($.tmpl(tmpl, data))
					
					
					
				if ($("input[type='checkbox'],input[type='radio']").length > 0) {
                    $("input[type='checkbox'],input[type='radio']").not(".toggle input[type='checkbox']").iCheck({
                        checkboxClass: 'icheckbox',
                        radioClass: 'iradio',
                    }).on('ifClicked', function (event) {
                        var id = $(event.target).parents('tr').attr('data-id')
                        var allId = $(event.target).attr('id')
                        //全选与反选
                        if(!allId){
                        	addDomName(id)
                        }else{
                        	addDomName2()
                        }
                    	
                        if(allId=='allCheck'){
                        	
                           if(!$(event.target).prop('checked')){
                           	
                               $('.A1').iCheck('check')
                           }else{
                               $('.A1').iCheck('uncheck')
                           }
                        }
                    });
                }
				
				
                if (dontRenderPager) {
                    renderPager(data.totalRecord);
                }
                
                
			})
		})
	}
	function renderPager(totalRecord, nowPage) {
		
		$("div.page").pagination(totalRecord, appApi("PAGE_SIZE"), {
			
			callback: function(page_id, jq) {
				pageNumNow1 = page_id + 1;
				showTable(false, pageNumNow1);
				arrys = [];
                power = false
				$("#allCheck").iCheck('uncheck')
			}
		}, nowPage);
	};
	
	
	ztreeLoad()
 	function ztreeLoad(){
 		var zNodes = []
 		ad.selectList("USER_ZTREE_NAME",{},function(data){
 			for(var i=0;i<data.length;i++){
 				zNodes.push({id:data[i].id,pId:data[i].pId,name:data[i].name})
 			}
 			
 			
 			var setting = {
			    check: {
			      enable: true
			    },
			    data:{
			      simpleData: {
			        enable: true
			      }
			    },
			    callback: {
					onCheck: zTreeOnCheck
				}
			  };
			  
			function zTreeOnCheck(event, treeId, treeNode) {
				var ztreeId = []
    			ztreeId.push(treeNode.id);
    			if(treeNode.checked){
    				for(var i=0;i<ztreeId.length;i++){
    					if(newData.indexOf(ztreeId[i])==-1){newData.push(ztreeId[i])}
    				}
    			}else{
    				var del = treeNode.id;
    				var num =  newData.indexOf(del);
    				newData.splice(num,1);
    				
    			}
			};  
			$.fn.zTree.init($("#role"), setting, zNodes);
 		})
 	
}
	  	


	

})